#!/bin/bash

# modify /etc/inittab so that testcloud client will be automatically run

if ! [ -f /etc/inittab ]; then
   echo "/etc/inittab not found ! Aborting."
   exit 1
fi

tf=`mktemp /tmp/inittab.XXXX`

# so that when uninstalling everything will go back
cp /etc/inittab /etc/inittab.orig

while read line
do
   t1=`echo ${line} | grep tty1`

   if [ "${t1}" != "" ]; then
      x=`echo ${line} | awk -F ":" 'BEGIN {l="";} { for (i=1; i<=NF; i++){ if (i!=NF) {l=l$i":";} else l=l"/usr/bin/testcloud-vm-init.sh";} } END {print l}'`
      echo ${x} >> ${tf}
   else
      echo ${line} >> ${tf}
   fi
done < /etc/inittab

cp -f ${tf} /etc/inittab
rm -f ${tf}

exit 0
